package com.ww.controller;


import com.github.pagehelper.PageInfo;
import com.ww.pojo.Examine;
import com.ww.pojo.Wallpaper;
import com.ww.service.ExamineService;
import com.ww.service.WallpaperService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/examine")
public class examineController {

    /**
     * 服务对象
     */
    @Resource
    private ExamineService examineService;
    @Resource
    private WallpaperService wallpaperService;
    /**
     * 插入审核结果
     * @param examine
     * @param httpSession
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Examine insertexamine(@RequestBody Examine examine, HttpSession httpSession){

        examine.setExamineTime(new Date());         //获取事件
        examine.setUserId(Integer.parseInt( httpSession.getAttribute("userId").toString()));       //获取用户id
        this.examineService.insertexamine(examine);             //执行插入
        Wallpaper wallpaper = new Wallpaper();                  //新建wallpaper对象
        wallpaper.setWallpaperId(examine.getWallpaperId());             //给wallpaper对象设置wallpaperid
        wallpaper.setWallpaperState(examine.getExamineResult());        //设置审核结果
        this.wallpaperService.update(wallpaper);                        //执行更新wallpaper表
        //System.out.println(wallpaper);                                  //打印测试
        return examine;                                                 //返回对象，后续修改
    }

    @RequestMapping("/deletewallpaper")
    public String deleteById(@RequestParam(value = "id") String id,
                             @RequestParam(value = "pagenum") String pagenum,
                             Model model) {
        wallpaperService.deleteById(Integer.valueOf(id));
        System.out.println("删除ID为"+id+"的壁纸");
        PageInfo<Wallpaper> wallpaperpage=wallpaperService.wallpaperpageBystate(Integer.valueOf(pagenum),5);
        model.addAttribute("wallpaperpage",wallpaperpage);
        return "admin/examples/wallpaper_examine";
    }
}
